package com.wifirecommenderap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ToggleButton;
import android.widget.EditText;
import android.widget.TextView;
import java.lang.Thread;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Intent;
import android.content.Context;
import android.content.ActivityNotFoundException;
import android.content.SharedPreferences;

public class WifiRecommenderAPActivity extends Activity 
{
	
	private boolean LoggingActive = false;
	//private TimerTask tt;
	private Timer t;
	//Thread loggingThread;
	//String loggingThreadName = "LoggingThread";
	private ScanningTask st;
	
	//TextView sv = (TextView) findViewById(R.id.status_textview);
	private TextView sv;// = (TextView) findViewById(R.id.status_textview);
	private TextView lstv;//logging_status_tv
	public TextView loggingTimerTaskTextView;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
   
        ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
        Button preferencesButton = (Button) findViewById(R.id.preferences_button);
        Button mapButton = (Button) findViewById(R.id.map_button);
        Button listButton = (Button) findViewById(R.id.list_button);
        
        sv = (TextView) findViewById(R.id.status_textview);
        lstv = (TextView) findViewById(R.id.logging_status_tv);
        
        setLoggingStatusMessage();
          
        //loggingThread = new Thread(tt, loggingThreadName);
        //loggingThread.setDaemon(true);
        
        //loggingTimerTaskTextView = (TextView)findViewById(R.id.logging_timertask_textview);
        //st = new ScanningTask();
        //t = new Timer("LoggingTimerTask", false);
        
        toggleButton.setOnClickListener(new ToggleButton.OnClickListener() 
        {
        	public void onClick(View v)
        	{
        		handleToggleButtonClick();
        	}
        });
        
        preferencesButton.setOnClickListener(new Button.OnClickListener() 
        {
        	public void onClick(View v)
        	{
        	//	handlePreferencesButtonClick();
        		Intent settingsActivity = new Intent(getBaseContext(),
                        PreferencesActivity.class);
        		startActivity(settingsActivity);
        	}
        });
        
        mapButton.setOnClickListener(new Button.OnClickListener() 
        {
        	public void onClick(View v)
        	{
        		handleMapButtonClick();
        	}
        });
        
        listButton.setOnClickListener(new Button.OnClickListener() 
        {
        	public void onClick(View v)
        	{
        		handleListButtonClick();
        	}
        });
        
        
        

        
        

        
    }
    
    private void handleListButtonClick()
    {
    	sv.setText("NEXT PRESSED!");
    	
    	//this is where we invoke the next Activity
    	
    	//Intent intent = new Intent("com.wifirecommenderap.LISTVIEW_VIEW_LIST");
    	Intent intent = new Intent(WifiRecommenderAPActivity.this, ListViewActivity.class);

    	try
    	{
    		startActivity(intent);
    	}
    	catch(ActivityNotFoundException anfe)
    	{
    		popupAlert("Exception", "Activity Not Found");
    	}
    }


    public void popupAlert(String Title, String Msg)
    {
    	
    	new AlertDialog.Builder(this).setTitle(Title).setMessage(Msg).setPositiveButton("Continue",
                new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int arg1) {
                        // in this case, don't need to do anything other than close alert
                    }
                }).show();
    	
    }
    
   // private void handlePreferencesButtonClick()
   // {
   // 	sv.setText("PREFERENCES PRESSED!");
   // 	//launch preferences view here
    	
   // 	popupAlert("Preferences", "Launch Preferences not yet implemented");
   // }

    private void handleMapButtonClick()
    {
    	sv.setText("MAP PRESSED!");
    	//launch preferences view here
    	
    	//popupAlert("MapView", "Launch MapView not yet implemented");
    	
    	Intent intent = new Intent(WifiRecommenderAPActivity.this, MapViewActivity.class);
   		startActivity(intent);
  	
    }
    

    private void setLoggingStatusMessage()
    {
        String LoggingActiveMessage = "Logging is Active";
        String LoggingInactiveMessage = "Logging is Not Active";
        
        if (LoggingActive)
        	lstv.setText(LoggingActiveMessage);
        else
        	lstv.setText(LoggingInactiveMessage);
     }
    
    private void handleToggleButtonClick()
    {
    	sv.setText("TOGGLE PRESSED!");
    	
    	if (LoggingActive)
    	{
    		//loggingThread.stop();   //StopLogging
    		//loggingTimerTaskTextView.setText( st.getCount() + " seconds");
    		//t.cancel();
    		//t.purge();
    		
    	}
    	else
    	{
    		popupAlert("LOGGING", "Logging is not enabled yet");
    		
    		try
    		{
    			//loggingThread.start();  //StartLogging
    	        
    			//st = new ScanningTask();
    	        //t = new Timer("LoggingTimerTask", false);
    			//t.scheduleAtFixedRate(st, 0, 1000);
    		}
    		catch(Exception e)
    		{
    			
    		}
    	}
    		
  
    	LoggingActive = !LoggingActive; 
    	setLoggingStatusMessage();
    }


}